package com.bj58.data.emailreports.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bj58.data.emailreports.dao.SrcDbPool.DBType;
import com.bj58.data.emailreports.model.partnershipModel;

import com.bj58.data.emailreports.service.PartnershipService;
import com.bj58.data.emailreports.utils.CommonDataDeal;
import com.bj58.data.emailreports.utils.SortUtil;

public class PartnershipServiceImpl implements PartnershipService {
	
	private CommonDataDeal cdd = new CommonDataDeal();
	
	@Override
	public List<partnershipModel> getLianmengData(String platform, String nowDate,String lastDate,String lastWeek) throws Exception{
	
		List<partnershipModel> finalList = new ArrayList<partnershipModel>();
		List<String> gbList =new ArrayList<String>();
		gbList.add("statDate");
		gbList.add("source");
		
		Map<String,List<String>> sqlMap = new HashMap<String,List<String>>();
		StringBuffer sql = new StringBuffer();
		List<String> dateList = new ArrayList<String>();
		dateList.add(nowDate);
		dateList.add(lastDate);
		dateList.add(lastWeek);
			
		List<String> cols1=new ArrayList<String>();
		cols1.add("statDate");		
		cols1.add("source");cols1.add("uv");cols1.add("pv");cols1.add("vppv");cols1.add("vppv_uv");cols1.add("out_rate");
		cols1.add("info_count");cols1.add("cpc_uv");cols1.add("ROI");cols1.add("cpc_cash");cols1.add("zhineng_cash");
		cols1.add("zhaopin_vppv");cols1.add("fangchan_vppv");cols1.add("ershouche_vppv");cols1.add("ershou_vppv");cols1.add("huangye_vppv");
		
		for (String date : dateList) {		
			sql.delete(0, sql.length()+1);
			sql.append(" select stat_date,source2, uv, pv, vppv,vppv_uv, out_rate, info_count,cpc_uv, ROI, cpc_cash,zhineng_cash, ");
			sql.append(" zhaopin_vppv, fangchan_vppv, ershouche_vppv, ershou_vppv, huangye_vppv ");	
			sql.append(" from t_netflow_source_ROImonitor");
			sql.append(" where stat_date = '"+date+"' ");
			sql.append(" and platform='"+platform+"'");		
			sql.append(" and source1='lianmeng' and source2<>'all' ");
			sql.append(" group by source2 ");
			sql.append(" order by uv desc");
			sql.append(" :"+DBType.db_94_data_app.getValue());
			sqlMap.put(sql.toString(), cols1);
		}

		finalList = cdd.AssembledDataBySqlMap(sqlMap, partnershipModel.class, gbList);
		return finalList;
	}
	

	@Override
	public List<partnershipModel> getonlineMakectData(String platform, String nowDate,String lastDate,String lastWeek) throws Exception{
	
		List<partnershipModel> finalList = new ArrayList<partnershipModel>();
		List<String> gbList =new ArrayList<String>();
		gbList.add("statDate");
		gbList.add("source");
		
		Map<String,List<String>> sqlMap = new HashMap<String,List<String>>();
		StringBuffer sql = new StringBuffer();
		List<String> dateList = new ArrayList<String>();
		dateList.add(nowDate);
		dateList.add(lastDate);
		dateList.add(lastWeek);
			
		List<String> cols1=new ArrayList<String>();
		cols1.add("statDate");		
		cols1.add("source");cols1.add("uv");cols1.add("pv");cols1.add("vppv");cols1.add("vppv_uv");cols1.add("out_rate");
		cols1.add("info_count");cols1.add("cpc_uv");cols1.add("ROI");cols1.add("cpc_cash");cols1.add("zhineng_cash");
		cols1.add("zhaopin_vppv");cols1.add("fangchan_vppv");cols1.add("ershouche_vppv");cols1.add("ershou_vppv");cols1.add("huangye_vppv");
		
		for (String date : dateList) {			
			sql.delete(0, sql.length()+1);
			sql.append(" select stat_date,source2, uv, pv, vppv,vppv_uv, out_rate, info_count,cpc_uv, ROI, cpc_cash,zhineng_cash, ");
			sql.append(" zhaopin_vppv, fangchan_vppv, ershouche_vppv, ershou_vppv, huangye_vppv ");	
			sql.append(" from t_netflow_source_ROImonitor");
			sql.append(" where stat_date = '"+date+"' ");
			sql.append(" and platform='"+platform+"'");
			
			if("-1".equals(platform))
			{
				sql.append(" and source1 in ('广告系列','SEM') ");
			}
			else 
			{
				sql.append(" and source1 in ('fufeiliulanqi','fufeiqita','SEM') ");
			}
			sql.append(" and source2<>'all' ");
			sql.append(" group by source2 ");
			sql.append(" order by uv desc");
			sql.append(" :"+DBType.db_94_data_app.getValue());
			sqlMap.put(sql.toString(), cols1);
		}

		finalList = cdd.AssembledDataBySqlMap(sqlMap, partnershipModel.class, gbList);
		return finalList;
	}
}
